package com.example.mobile2121606109ycg0516;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {

    private TabLayout tlMainnnb;
    private ViewPager2 bpMainnnb;
    List<Fragment>fragmentsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();

        fragmentsList.add(NewsFragment.newInstance("新闻",""));
        fragmentsList.add(NoticesFragment.newInstance("通知",""));
        fragmentsList.add(BulletinsFragment.newInstance("公告",""));

        ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager(),getLifecycle());
        adapter.setFragmentsList(fragmentsList);
        bpMainnnb.setAdapter(adapter);

        new TabLayoutMediator(tlMainnnb,bpMainnnb,true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(fragmentsList.get(position).getArguments().getString("title"));
            }
        }).attach();
    }

    private void initView() {
        tlMainnnb = (TabLayout) findViewById(R.id.tlMainnnb);
        bpMainnnb = (ViewPager2) findViewById(R.id.bpMainnnb);
    }
}